package com.yonyou.cyxdms.sysAuth.service.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yonyou.cyx.framework.bean.dto.base.BaseDTO;
import com.yonyou.cyx.function.utils.bean.BeanMapperUtil;
import com.yonyou.cyxdms.framework.bean.entity.base.DMSBasePO;
import com.yonyou.cyxdms.sysAuth.service.dto.user.UserDTO;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 账号信息
 * </p>
 *
 * @author adu
 * @since 2019-08-27
 */
@TableName("tm_user")
public class UserPO extends DMSBasePO<UserPO> {

    private static final long serialVersionUID = 1L;

    /**
     * 账号ID
     */
    @TableId(value = "USER_ID", type = IdType.AUTO)
    private Integer userId;
    /**
     *
     */
    @TableField("EMP_ID")
    private Integer empId;
    /**
     * 租户id
     */
    @TableField("APP_ID")
    private String appId;

    /**
     * 作为登陆的ID
     */
    @TableField("COMPANY_ID")
    private Long companyId;



    @TableField("USER_CODE")
    private String userCode;

    /**
     * 密码
     */
    @TableField("PASSWORD")
    private String password;

    /**
     * 停用，起用
     */
    @TableField("USER_STATUS")
    private Integer userStatus;

    /**
     * 最后登录时间
     */
    @TableField("LOGIN_LAST_TIME")
    private LocalDateTime loginLastTime;

    /**
     * 登录失败次数
     */
    @TableField("FAILURE_NUM")
    private Integer failureNum;

    @TableField("LOCK_FLAG")
    private Integer lockFlag;


    @TableField("STOP_DATE")
    private LocalDateTime stopDate;

    @TableField("SOURCE_CHANGED_AT")
    private LocalDateTime sourceChangedAt;

    @TableField("RECORD_VERSION")
    private Integer recordVersion;

    @TableField("REG_ID")
    private String regId;

    /**
     * 最后登录时间
     */
    @TableField("UPDATE_PASSWORD_DATE")
    private LocalDateTime updatePasswordDate;

    public LocalDateTime getUpdatePasswordDate() {
        return updatePasswordDate;
    }

    public void setUpdatePasswordDate(LocalDateTime updatePasswordDate) {
        this.updatePasswordDate = updatePasswordDate;
    }

    public String getRegId() {
        return regId;
    }

    public void setRegId(String regId) {
        this.regId = regId;
    }
    @TableField("LOCK_CAUSE")
    private String lockCause;
    public UserPO() {
        super();
    }

    public String getLockCause() {
        return lockCause;
    }

    public void setLockCause(String lockCause) {
        this.lockCause = lockCause;
    }

    @Override
    public String toString() {
        return "UserPO{" +
                "userId=" + userId +
                ", empId=" + empId +
                ", appId='" + appId + '\'' +
                ", companyId='" + companyId + '\'' +
                ", userCode='" + userCode + '\'' +
                ", password='" + password + '\'' +
                ", userStatus=" + userStatus +
                ", loginLastTime=" + loginLastTime +
                ", failureNum=" + failureNum +
                ", lockFlag=" + lockFlag +
                ", stopDate=" + stopDate +
                ", sourceChangedAt=" + sourceChangedAt +
                ", recordVersion=" + recordVersion +
                ", regId='" + regId + '\'' +
                '}';
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getAppId() {
        return appId;
    }

    public Long getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getUserStatus() {
        return userStatus;
    }

    public void setUserStatus(Integer userStatus) {
        this.userStatus = userStatus;
    }

    public LocalDateTime getLoginLastTime() {
        return loginLastTime;
    }

    public void setLoginLastTime(LocalDateTime loginLastTime) {
        this.loginLastTime = loginLastTime;
    }

    public Integer getFailureNum() {
        return failureNum;
    }

    public void setFailureNum(Integer failureNum) {
        this.failureNum = failureNum;
    }

    public Integer getLockFlag() {
        return lockFlag;
    }

    public void setLockFlag(Integer lockFlag) {
        this.lockFlag = lockFlag;
    }

    public LocalDateTime getStopDate() {
        return stopDate;
    }

    public void setStopDate(LocalDateTime stopDate) {
        this.stopDate = stopDate;
    }

    public LocalDateTime getSourceChangedAt() {
        return sourceChangedAt;
    }

    public void setSourceChangedAt(LocalDateTime sourceChangedAt) {
        this.sourceChangedAt = sourceChangedAt;
    }

    @Override
    public Integer getRecordVersion() {
        return recordVersion;
    }

    @Override
    public void setRecordVersion(Integer recordVersion) {
        this.recordVersion = recordVersion;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public UserPO(UserDTO dto) {
        this.userId = dto.getUserId() == null ? null : Integer.parseInt(String.valueOf(dto.getUserId()));
        this.userCode = dto.getUserCode();
        this.userStatus = dto.getUserStatus();
        this.password = dto.getPassword();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        this.loginLastTime = dto.getLoginLastTime() == null ? null : LocalDateTime.parse(dto.getLoginLastTime(), df);
        this.stopDate = dto.getStopDate() == null ? null : LocalDateTime.parse(dto.getStopDate(), df);
//        this.failureNum = ;
//        this.lockFlag = lockFlag;
    }



    /**
     * 将PO 信息转化为DTO
     *
     * @param dto 需要进行转换的dto
     * @author quankk
     * @since 2018/7/22 0022
     */
    protected <T extends BaseDTO> void transDtoToPo(T dto) {
        BeanMapperUtil.copyProperties(this, dto, "userId");
    }

}
